from app.core.crud import CRUDBase
from app.models.category import Category
from app.schemas.category import CategoryCreate, CategoryUpdate


class CategoryController(CRUDBase[Category, CategoryCreate, CategoryUpdate]):
    def __init__(self):
        super().__init__(model=Category)

    async def is_exist(self, name: str) -> bool:
        return await self.model.filter(name=name).exists()

    async def update_category(self, id: int, obj_in: CategoryUpdate):
        category = await self.model.get(id=id)
        category.name = obj_in.name
        category.code = obj_in.code
        category.desc = obj_in.desc
        category.status = obj_in.status
        category.order = obj_in.order
        await category.save()


category_controller = CategoryController()
